package com.example.testinternet;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.testinternet.bean.NewsBean;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.image.SmartImageView;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.internal.spdy.FrameReader;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;

public class NewsActivity extends AppCompatActivity {

    private ListView lvNEws;
    private  String json="";
    ArrayList<NewsBean>news;
    private OkHttpClient client;
    private LinearLayout layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);
        lvNEws = (ListView) findViewById(R.id.lv_news);


        getNewsData();



    }

    private void getNewsData() {
        //json
         String ip="http://10.0.2.2:8080/news/news.json";
            //1-创建okhttpclient
        client = new OkHttpClient();
            //2-创建一个请求对象
            Request request=new Request.Builder().url(ip).build();
            //3-客户端创建请求
            Call call= client.newCall(request);
            //4-将请求添加到调度队列
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Request request, IOException e) {
                }
                @Override
                public void onResponse(Response response) throws IOException {
                    json=new String(response.body().bytes(),"utf-8");
                    //解析json

                    parsJson(json);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            lvNEws.setAdapter(new MyAdapter());
                        }
                    });



                }
            });

    }

    private void parsJson(String json) {
        Log.i("NewsActivity", "parsJson: "+json);
        Gson gson=new Gson();
        Type listtppe=new TypeToken<ArrayList<NewsBean>>(){}.getType();
        news=gson.fromJson(json,listtppe);
    }
    private class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            Log.i("NewsActivity", "------onResponse: "+  news.size());
            return news.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(  int position, View convertView, ViewGroup parent) {
            View view=View.inflate(getApplicationContext(),R.layout.news_item,null);
            SmartImageView icon= (SmartImageView) view.findViewById(R.id.siv_icon);
            TextView title= (TextView) view.findViewById(R.id.tv_title);
            //第一个参数，下载的图片地址，参数2-下载失败的时显示的图片，参数3-下载中显示的图片
            icon.setImageUrl(news.get(position).getIcon());

            title.setText(news.get(position).getTitle());

            return view;

        }
    }





}
